<?php
//浏览历史
class App_History extends Data_Base
{
	//默认地址
    public static function defaultAddr ($user)
    {
        $addrs = self::infoList($user);
        if (! empty($addrs))
            return $addrs[0];
        else
            return NULL;
    }

	//设置默认
	//$user object 用户对象
	//$nid	int 浏览历史id
    public static function setDefault($user, $nid)
    {
        $rows = DB::update('user_v2_history')
            ->set(array('isDefault' => 0))
            ->where('uid', '=', $user->getUserUid())
            ->execute();

        $rows = DB::update('user_v2_history')
            ->set(array('isDefault' => 1))
            ->where('uid', '=', $user->getUserUid())
            ->where('id', '=', $nid)
            ->execute();
    }

	//浏览详细
	//id int 浏览历史id
    public static function detail ($id) 
    {
        $rs = self::item('user_v2_history', 'id', $id);
        $rs['proviceName'] = App_Region::name($rs['proviceId']);
        $rs['cityName']    = App_Region::name($rs['cityId']);
        $rs['countryName'] = App_Region::name($rs['countryId']);

        return $rs;
    }

	//浏览列表
	//$user object 用户对象
    public static function infoList ($user, $limit = 10)
    {
        $uid = $user->getUserUid();
        if ($uid == 0)
            return array();

        $query = DB::select()
            ->from('user_v2_history')
            ->where('uid', '=', $uid)
            ->order_by('created', 'desc')
            ->limit($limit)
            ->execute();

        $ret = array();
        foreach ($query as $rs)
        {
            $type = $rs['type'];
            $nid  = $rs['nid'];

            if ($type == 1)
            {
                $goods = App_Goods::basicInfo($nid);
                $rs['goods'] = $goods;
                $ret[] = $rs;
            }
        }

        return $ret;
    }

	//编辑浏览历史
	//$user		object		用户对象
	//$nid		int			浏览历史id
	//$data		array		浏览信息
    public static function edit ($user, $nid, $data)
    {
        $row = self::buildDataFromRawInput($data);

        unset($row['id']);
        unset($row['uid']);
        unset($row['created']);
        $row['changed'] = DB::expr('unix_timestamp(now())');

        $rows = DB::update('user_v2_history')
            ->set($row)
            ->where('uid', '=', $user->getUserUid())
            ->where('id', '=', $nid)
            ->execute();

        return $rows > 0;
    }

	//添加浏览历史
	//$user		object	用户对象
	//$data		array	浏览信息
    public static function add ($user, $data)
    {
        $row = self::buildDataFromRawInput($data);

        unset($row['id']);
        $row['uid'] = $user->getUserUid();
        $row['created'] = DB::expr('unix_timestamp(now())');
        $row['changed'] = DB::expr('unix_timestamp(now())');

        $counts = self::counts('user_v2_history', array(
                array('uid', '=', $row['uid']),
                array('nid', '=', $row['nid']),
                array('type', '=', $row['type']),
            ));

        if ($counts == 0)
        {
            list($insert_id, $rows) = DB::insert('user_v2_history')
                ->columns (array_keys($row))
                ->values (array_values($row))
                ->execute();
            return $insert_id;
        }

        return FALSE;

    }

	//移除浏览历史
	//$user object 用户对象
	//$nid	int    浏览历史id
    public static function remove($user, $nid)
    {
        $rows = DB::delete('user_v2_history')
            ->where('uid', '=', $user->getUserUid())
            ->where('id', '=', $nid)
            ->limit(1)
            ->execute();

        return $rows > 0;
    }

	//构造格式化信息
    protected static function buildDataFromRawInput ($data)
    {
        $ret = array();
        $ret['type'] = 1;
        $ret['nid']  = (int) Arr::get($data, 'nid');

        return $ret;
    }
}
